package section

import (
	"context"

	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/enum/errorcode"
	"mall/service/backend/common/errorx"
	"mall/service/backend/rpc/backendclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type AttachPostToSectionLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAttachPostToSectionLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AttachPostToSectionLogic {
	return &AttachPostToSectionLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AttachPostToSectionLogic) AttachPostToSection(
	req *types.AttachKBPostToSectionReq,
) (*types.BaseMsgResp, error) {
	if req.SectionId == nil || req.PostId == nil || req.PostType == nil || *req.SectionId == 0 || *req.PostId == 0 || *req.PostType == 0 {
		return nil, errorx.NewCodeError(errorcode.InvalidArgument,
			l.svcCtx.Trans.Trans(l.ctx, "common.validationError"))
	}

	res, err := l.svcCtx.BackendRpc.AttachPostToSection(l.ctx, &backendclient.SectionPostInfo{
		SectionId: req.SectionId,
		PostId:    req.PostId,
		PostType:  req.PostType,
		Sort:      req.Sort,
	})
	if err != nil {
		return nil, err
	}

	return &types.BaseMsgResp{
		Success: true,
		Msg:     l.svcCtx.Trans.Trans(l.ctx, res.Msg),
	}, nil
}
